//
//  LBLeftTableView.m
//  loveBeen
//
//  Created by guofei on 2016/11/19.
//  Copyright © 2016年 loveBeen. All rights reserved.
//

#import "LBLeftTableView.h"

#import "LBLeftCell.h"

#import "LBSuperMarketProducts.h"
#import "LBGetDataTool.h"

static NSString *LEFTCELL = @"leftcell";



@interface LBLeftTableView ()<UITableViewDelegate,UITableViewDataSource>



@end

@implementation LBLeftTableView{

    NSInteger _flag;
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        _flag = 0;
        [self setupUI];
    }
    return self;
}

#pragma mark - set  方法中刷新 UI
-(void)setMarketModelList:(NSArray<LBSuperMarketCategory *> *)marketModelList{

    _marketModelList = marketModelList;
    
    //  刷新 UI
    [self reloadData];

}

#pragma mark - 设置界面
- (void)setupUI{
    
    [self registerNib:[UINib nibWithNibName:@"LBLeftCell" bundle:nil] forCellReuseIdentifier:LEFTCELL];
    
    // 隐藏垂直滚动条
    self.showsVerticalScrollIndicator = NO;
    
    // 代理
    self.delegate = self;
    self.dataSource = self;
    
    // 设置 footerView 占位
    self.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 60)];

    
    if (self.leftRow) {
        
        NSIndexPath *idexPath = [NSIndexPath indexPathForRow:self.leftRow inSection:0];
        
        [self scrollToRowAtIndexPath:idexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
    }
}



#pragma mark - datasource
// row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _marketModelList.count;

}

// cell --  显示新的 cell 时候才会调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    LBLeftCell *leftCell = [self dequeueReusableCellWithIdentifier:LEFTCELL forIndexPath:indexPath];
    
    
    leftCell.model = _marketModelList[indexPath.row];
    
    
    if (self.leftRow) {
        
        if (self.leftRow == indexPath.row) {
            
            NSInteger row = self.leftRow;
            

            
            [[NSNotificationCenter defaultCenter]postNotificationName:RowNum object:self userInfo:@{RowNum:@(row)}];
            
            leftCell.yellowView.hidden = NO;
            leftCell.selectView.hidden = NO;
            
        }
        
    }else{
    
        if (indexPath.row == _flag) {
            _flag = -1;
            
            leftCell.yellowView.hidden = NO;
            leftCell.selectView.hidden = NO;

        }else if(_flag == -1){
        
            leftCell.yellowView.hidden = YES;
            leftCell.selectView.hidden = YES;

        }
    
    }
    return leftCell;
}


#pragma mark - delegate 
// 选中的时候调用
// MARK: - 发送 row 通知 . 设置小黄条 隐藏时机
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (self.leftRow) {
        
        NSIndexPath *idxPath = [NSIndexPath indexPathForRow:self.leftRow inSection:0];

        [self setYellowWithIndexPatn:idxPath Hidden:YES];
        
        self.leftRow = 0;
        
    }

    // 通知
    NSInteger row = indexPath.row;
    [[NSNotificationCenter defaultCenter]postNotificationName:RowNum object:self userInfo:@{RowNum:@(row)}];
    
    // 如果选中不是第 0 行, 就隐藏第 0 行
    if (indexPath.row) {
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        
        [self setYellowWithIndexPatn:indexPath Hidden:YES];
        
    }
    
    // 显示选中的 小黄条
    
    [self setYellowWithIndexPatn:indexPath Hidden:NO];
    
}

// 隐藏取消选中的 小黄条
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self setYellowWithIndexPatn:indexPath Hidden:YES];

}


- (void)setYellowWithIndexPatn:(NSIndexPath *)indexPath Hidden:(BOOL)hidden{

    LBLeftCell *cell = (LBLeftCell *)[self cellForRowAtIndexPath:indexPath];
    cell.yellowView.hidden = hidden;
    cell.selectView.hidden = hidden;

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
